package com.store.chap1;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.jdbc.SqlRunner;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import java.util.Map;

/**
 * @author mmzhang
 * @date 2021年08月18日 20:28
 * @Description:
 */
public class Example01 {

    private Connection conn =null;

    @Before
    public void initData(){
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            conn=DriverManager.getConnection("jdbc:hsqldb:mem:mybatis","sa","");
            ScriptRunner scriptRunner=new ScriptRunner(conn);
            scriptRunner.setLogWriter(null);
            scriptRunner.runScript(Resources.getResourceAsReader("hsqldb/create-table.sql"));
            scriptRunner.runScript(Resources.getResourceAsReader("hsqldb/init-data.sql"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 测试能否执行数据库脚本与查询语句
     */
    @Test
    public void testHsqldbQuery(){
        SqlRunner sqlRunner=new SqlRunner(conn);
        try {
            List<Map<String, Object>> result = sqlRunner.selectAll("select * from user");
            result.forEach(System.out::println);
            sqlRunner.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
